package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Site;
import org.millioncall.yueyoga.admin.service.SystemService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

//--created by lisj on 2014.03.06
@Controller
public class SystemAction extends BaseAction {
	private SystemService systemService;

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	// 编辑系统设置
	@RequestMapping(value = "/ajaxeditsystem.do")
	public void toEeditSystem(int recommendScore, double toScore,
			double toMoney, int id, HttpServletRequest request,
			HttpServletResponse response) {
		String msg = null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("toScore", toScore);

		if (systemService.updateSystem(map)) {
			msg = "{'success':true,'msg':'系统设置保存成功'}";
		} else {
			msg = "{'success':false,'msg':'系统设置保存失败，请稍候重试或联系管理员'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	// 编辑系统设置
	@RequestMapping(value = "/ajaxeditsystem.jhtml")
	public void editSystem(double toScore, int id, int reserveCount, HttpServletRequest request,
			HttpServletResponse response) {
		String msg = null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("toScore", toScore);
		map.put("reserveCount", reserveCount);

		if (systemService.updateSystem(map)) {
			msg = "{\"success\":true,\"msg\":\"系统设置保存成功!\"}";
		} else {
			msg = "{\"success\":false,\"msg\":\"系统设置保存失败，请稍候重试或联系管理员!\"}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	// 查询系统设置
	@RequestMapping(value = "/ajaxgetsystem.do")
	public void getSystem(HttpServletRequest request,
			HttpServletResponse response) {
		String msg = null;
		Site site = systemService.getSystem();
		msg = "{'success':true,'id':" + site.getId() + ",'toScore':"
				+ site.getToScore() + ",'toMoney':" + site.getToMoney()
				+ ",'recommendScore':" + site.getRecommendScore() + "}";
		WebAppUtils.ajaxResponse(response, msg);
	}

	// 查询系统设置
	@RequestMapping(value = "/toSystem.jhtml")
	public String toSystem(ModelMap context, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, context);

		Site site = systemService.getSystem();
		context.put("site", site);
		return "system";
	}

}
